package com.ch_linghu.fanfoudroid.fanfou;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.database.Cursor;
import android.util.Log;

import com.ch_linghu.fanfoudroid.db.BusTable;
import com.ch_linghu.fanfoudroid.db.UserInfoTable;
import com.ch_linghu.fanfoudroid.http.HttpException;
import com.ch_linghu.fanfoudroid.http.Response;

public class Bus extends WeiboResponse implements Serializable {
	
	private String bid;
	private String name;
	private String city;
	private String location;
	private String path;
	private String intro;
	private String announce;
	private int memberCount;
	
	/* package */Bus(Response res) throws HttpException {
		super();
		//Element elem = res.asDocument().getDocumentElement();
		init(res.asJSONObject());
	}
	
	/* package */Bus(JSONObject json)
	throws HttpException {
	 
		init(json);
		}
	
	Bus(){
		
	}
	public Bus(Response res, Element elem)throws HttpException {
		super(res);
		init(elem);
	}
	
	private void init(JSONObject json) throws HttpException {
		try {
			bid =json.getString("id");
			name =json.getString("name");
			city =json.getString("city");
			location =json.getString("location");
			path =json.getString("path");
			intro =json.getString("intro");
			announce =json.getString("announce");
			memberCount =json.getInt("membercount");
			
		} catch (JSONException jsone) {
			// TODO Auto-generated catch block
			throw new HttpException(jsone.getMessage() + ":" + json.toString(),
					jsone);
		}
	}
	
	private void init(Element elem) throws HttpException {
		
		bid = getChildString("id",elem);
		name = getChildString("name",elem);
		city = getChildString("city",elem);
		location = getChildText("location", elem);
		path = getChildString("path",elem);
		intro = getChildString("intro",elem);
		announce = getChildString("announce",elem);
		memberCount = getChildInt("membercount",elem);
		
	}
	
	public static List<Bus> constructBuses(Response res) throws HttpException{
		Document doc = res.asDocument();
		if (isRootNodeNilClasses(doc)) {
			return new ArrayList<Bus>(0);
		} else {
			
			NodeList list = doc.getDocumentElement().getChildNodes();
			List<Bus> buses = new ArrayList<Bus>(list.getLength());
			Node node;
			for (int i = 0; i < list.getLength(); i++) {
				node = list.item(i);
				if (node.getNodeName().equals("user")) {
					buses.add(new Bus(res, (Element) node));
				}
			}

			return buses;
			
		}
		
	}
	
//	public static BusWapper constructWapperBuses(Response res)
//		throws HttpException {
//			Document doc = res.asDocument();
//			if (isRootNodeNilClasses(doc)) {
//				return new BusWapper(new ArrayList<Bus>(0), 0, 0);
//			} else {
//				try {
//					ensureRootNodeNameIs("users_list", doc);
//					Element root = doc.getDocumentElement();
//					NodeList bus = root.getElementsByTagName("buses");
//					int length = bus.getLength();
//					if (length == 0) {
//						return new BusWapper(new ArrayList<Bus>(0), 0, 0);
//					}
//					//
//					Element listsRoot = (Element) bus.item(0);
//					NodeList list = listsRoot.getChildNodes();
//					List<Bus> buses = new ArrayList<Bus>(list.getLength());
//					Node node;
//					for (int i = 0; i < list.getLength(); i++) {
//						node = list.item(i);
//						if (node.getNodeName().equals("user")) {
//							buses.add(new Bus(res, (Element) node));
//						}
//					}
//					//
//					long previousCursor = getChildLong("previous_curosr", root);
//					long nextCursor = getChildLong("next_curosr", root);
//					if (nextCursor == -1) { // 兼容不同标签名称
//						nextCursor = getChildLong("nextCurosr", root);
//					}
//					return new BusWapper(buses, previousCursor, nextCursor);
//				} catch (HttpException te) {
//					if (isRootNodeNilClasses(doc)) {
//						return new BusWapper(new ArrayList<Bus>(0), 0, 0);
//					} else {
//						throw te;
//					}
//				}
//			}
//
//	}
	
	public static List<User> constructUsers(Response res) throws HttpException {
		try {
			JSONArray list = res.asJSONArray();
			int size = list.length();
			List<User> users = new ArrayList<User>(size);
			for (int i = 0; i < size; i++) {
				users.add(new User(list.getJSONObject(i)));
			}
			return users;
		} catch (JSONException jsone) {
			throw new HttpException(jsone);
		} catch (HttpException te) {
			throw te;
		}
	}

	/**
	 * 
	 * @param res
	 * @return
	 * @throws HttpException
	 */
	public static BusWapper constructWapperBuses(Response res)
			throws HttpException {
		JSONObject jsonUsers = res.asJSONObject(); // asJSONArray();
		try {
			JSONArray user = jsonUsers.getJSONArray("buses");
			int size = user.length();
			List<Bus> users = new ArrayList<Bus>(size);
			for (int i = 0; i < size; i++) {
				users.add(new Bus(user.getJSONObject(i)));
			}
			long previousCursor = jsonUsers.getLong("previous_curosr");
			long nextCursor = jsonUsers.getLong("next_cursor");
			if (nextCursor == -1) { // 兼容不同标签名称
				nextCursor = jsonUsers.getLong("nextCursor");
			}
			return new BusWapper(users, previousCursor, nextCursor);
		} catch (JSONException jsone) {
			throw new HttpException(jsone);
		}
	}

	/**
	 * @param res
	 * @return
	 * @throws HttpException
	 */
	static List<Bus> constructResult(Response res) throws HttpException {
		JSONArray list = res.asJSONArray();
		try {
			int size = list.length();
			List<Bus> users = new ArrayList<Bus>(size);
			for (int i = 0; i < size; i++) {
				users.add(new Bus(list.getJSONObject(i)));
			}
			return users;
		} catch (JSONException e) {
		}
		return null;
	}
	
	
	
	public String getBid() {
		return bid;
	}
	public String getName() {
		return name;
	}
	public String getCity() {
		return city;
	}
	public String getLocation() {
		return location;
	}
	public String getPath() {
		return path;
	}
	public String getIntro() {
		return intro;
	}
	public String getAnnounce() {
		return announce;
	}
	public int getMemberCount() {
		return memberCount;
	}
	

	@Override
	public int hashCode() {
		return bid.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		return obj instanceof Bus && ((Bus) obj).bid.equals(this.bid);
	}
	
	public String toString() {
		return "User{" +  "bid=" + bid + ", name='" + name
		+ '\''+ ", city='"
		+ city + '\'' + ", location='" + location + '\''
		+ ", path='" + path + '\'' + ", intro='"
		+ intro + '\'' + ", announce=" + announce
		+ ", memberCount=" + memberCount +'\''+'}';
		
	}
	
	public static Bus  parseBus(Cursor cursor) {
		if (null == cursor || 0 == cursor.getCount() || cursor.getCount() > 1) {
			Log.w("User.ParseUser",
					"Cann't parse Cursor, bacause cursor is null or empty.");
		}
		cursor.moveToFirst();
		Bus b =new Bus();
		b.bid =cursor.getString(cursor.getColumnIndex(BusTable._ID));
		b.name =cursor.getString(cursor.getColumnIndex(BusTable.FIELD_NAME));
		b.city =cursor.getString(cursor.getColumnIndex(BusTable.FIELD_CITY));
		b.location =cursor.getString(cursor.getColumnIndex(BusTable.FIELD_LOCALTION));
		b.path =cursor.getString(cursor.getColumnIndex(BusTable.FIELD_PATH));
		b.intro =cursor.getString(cursor.getColumnIndex(BusTable.FIELD_INTRO));
		b.announce =cursor.getString(cursor.getColumnIndex(BusTable.FIELD_ANNOUNCE));
		b.memberCount =cursor.getInt(cursor.getColumnIndex(BusTable.FIELD_MEMBER_COUNT));
		
		return b;
	}
	
	public com.ch_linghu.fanfoudroid.data.Bus parseBus(){
		com.ch_linghu.fanfoudroid.data.Bus bus =new com.ch_linghu.fanfoudroid.data.Bus();
		bus.bid = this.bid;
		bus.name = this.name;
		bus.city = this.city;
		bus.location = this.location;
		bus.intro = this.intro;
		bus.path =this .path;
		bus.memberCount =this.memberCount;
		
		return bus;
	}
 
	
	

}
